package br.ufc.great.dtx.tm.lm;

/**
 * 
 * @author Leonardo Oliveira Moreira
 *
 * Classe utilizada para tratar a compatibilidade entre bloqueios
 */
public class XDGLLockCompatibility {

	public static boolean isCompatibility(int lock1, int lock2) {
		if (lock1 == XDGLLockType.NONE || lock2 == XDGLLockType.NONE)
			return true;
		/* SHARED_INTO */
		if (lock1 == XDGLLockType.SHARED_INTO)
			if (lock2 == XDGLLockType.SHARED_AFTER || 
				lock2 == XDGLLockType.SHARED_BEFORE || 
				lock2 == XDGLLockType.SHARED_TREE || 
				lock2 == XDGLLockType.INTENTION_SHARED || 
				lock2 == XDGLLockType.INTENTION_EXCLUSIVE)
				return true;
		if (lock2 == XDGLLockType.SHARED_INTO)
			if (lock1 == XDGLLockType.SHARED_AFTER ||
				lock1 == XDGLLockType.SHARED_BEFORE  || 
				lock1 == XDGLLockType.SHARED_TREE || 
				lock1 == XDGLLockType.INTENTION_SHARED || 
				lock1 == XDGLLockType.INTENTION_EXCLUSIVE)
				return true;
		/* SHARED AFTER */
		if (lock1 == XDGLLockType.SHARED_AFTER)
			if (lock2 == XDGLLockType.SHARED_INTO || 
				lock2 == XDGLLockType.SHARED_BEFORE || 
				lock2 == XDGLLockType.SHARED_TREE || 
				lock2 == XDGLLockType.INTENTION_SHARED)
				return true;
		if (lock2 == XDGLLockType.SHARED_AFTER)
			if (lock1 == XDGLLockType.SHARED_INTO || 
				lock1 == XDGLLockType.SHARED_BEFORE || 
				lock1 == XDGLLockType.SHARED_TREE || 
				lock1 == XDGLLockType.INTENTION_SHARED)
				return true;
		/* SHARED BEFORE */
		if (lock1 == XDGLLockType.SHARED_BEFORE)
			if (lock2 == XDGLLockType.SHARED_INTO || 
				lock2 == XDGLLockType.SHARED_AFTER || 
				lock2 == XDGLLockType.SHARED_TREE || 
				lock2 == XDGLLockType.INTENTION_SHARED || 
				lock2 == XDGLLockType.INTENTION_EXCLUSIVE)
				return true;
		if (lock2 == XDGLLockType.SHARED_BEFORE)
			if (lock1 == XDGLLockType.SHARED_INTO || 
				lock1 == XDGLLockType.SHARED_AFTER || 
				lock1 == XDGLLockType.SHARED_TREE || 
				lock1 == XDGLLockType.INTENTION_SHARED || 
				lock1 == XDGLLockType.INTENTION_EXCLUSIVE)
				return true;
		/* EXCLUSIVE */
		if (lock1 == XDGLLockType.EXCLUSIVE)
			if (lock2 == XDGLLockType.INTENTION_SHARED || 
				lock2 == XDGLLockType.INTENTION_EXCLUSIVE)
				return true;
		if (lock2 == XDGLLockType.EXCLUSIVE)
			if (lock1 == XDGLLockType.INTENTION_SHARED || 
				lock1 == XDGLLockType.INTENTION_EXCLUSIVE)
				return true;
		/* SHARED_TREE */
		if (lock1 == XDGLLockType.SHARED_TREE)
			if (lock2 == XDGLLockType.SHARED_INTO || 
				lock2 == XDGLLockType.SHARED_AFTER || 
				lock2 == XDGLLockType.SHARED_BEFORE || 
				lock2 == XDGLLockType.SHARED_TREE || 
				lock2 == XDGLLockType.INTENTION_SHARED)
				return true;
		if (lock2 == XDGLLockType.SHARED_TREE)
			if (lock1 == XDGLLockType.SHARED_INTO || 
				lock1 == XDGLLockType.SHARED_AFTER ||
				lock1 == XDGLLockType.SHARED_BEFORE || 
				lock1 == XDGLLockType.SHARED_TREE || 
				lock1 == XDGLLockType.INTENTION_SHARED)
				return true;
		/* EXCLUSIVE_TREE */
		/* Incompatível com todos bloqueios */
		/* INTENTION_SHARED */
		if (lock1 == XDGLLockType.INTENTION_SHARED)
			if (lock2 == XDGLLockType.SHARED_INTO || 
				lock2 == XDGLLockType.SHARED_AFTER || 
				lock2 == XDGLLockType.SHARED_BEFORE || 
				lock2 == XDGLLockType.EXCLUSIVE || 
				lock2 == XDGLLockType.SHARED_TREE || 
				lock2 == XDGLLockType.INTENTION_SHARED || 
				lock2 == XDGLLockType.INTENTION_EXCLUSIVE)
				return true;
		if (lock2 == XDGLLockType.INTENTION_SHARED)
			if (lock1 == XDGLLockType.SHARED_INTO || 
				lock1 == XDGLLockType.SHARED_AFTER || 
				lock1 == XDGLLockType.SHARED_BEFORE || 
				lock1 == XDGLLockType.EXCLUSIVE || 
				lock1 == XDGLLockType.SHARED_TREE ||
				lock1 == XDGLLockType.INTENTION_SHARED || 
				lock1 == XDGLLockType.INTENTION_EXCLUSIVE)
				return true;
		/* INTENTION_EXCLUSIVE */
		if (lock1 == XDGLLockType.INTENTION_EXCLUSIVE)
			if (lock2 == XDGLLockType.SHARED_INTO || 
				lock2 == XDGLLockType.SHARED_AFTER || 
				lock2 == XDGLLockType.SHARED_BEFORE || 
				lock2 == XDGLLockType.EXCLUSIVE || 
				lock2 == XDGLLockType.INTENTION_SHARED || 
				lock2 == XDGLLockType.INTENTION_EXCLUSIVE)
				return true;
		if (lock2 == XDGLLockType.INTENTION_EXCLUSIVE)
			if (lock1 == XDGLLockType.SHARED_INTO || 
				lock1 == XDGLLockType.SHARED_AFTER || 
				lock1 == XDGLLockType.SHARED_BEFORE || 
				lock1 == XDGLLockType.EXCLUSIVE || 
				lock1 == XDGLLockType.INTENTION_SHARED || 
				lock1 == XDGLLockType.INTENTION_EXCLUSIVE)
				return true;
		/* SHARED_DOCUMENT */
		if (lock1 == XDGLLockType.SHARED_DOCUMENT)
			if (lock2 == XDGLLockType.SHARED_DOCUMENT) 
				return true;
		if (lock2 == XDGLLockType.SHARED_DOCUMENT)
			if (lock1 == XDGLLockType.SHARED_DOCUMENT) 
				return true;

		return false;
	}
	
}
